package com.tronsis.distribution.activity.me.stock;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.tronsis.distribution.R;
import com.tronsis.distribution.bean.MyStockBean;
import com.tronsis.distribution.http.Url;
import com.tronsis.library.activity.ToolBarBaseActivity;
import com.tronsis.library.adapter.recyclerview.CommonAdapter;
import com.tronsis.library.adapter.recyclerview.MultiItemTypeAdapter;
import com.tronsis.library.adapter.recyclerview.base.ViewHolder;
import com.tronsis.library.util.DisplayUtil;
import com.tronsis.library.util.KeyboardUtils;
import com.tronsis.library.util.ToastUtil;
import com.tronsis.library.widget.ConfirmDialog;
import com.tronsis.library.widget.MyDecoration;
import com.tronsis.library.widget.RecycleViewDivider;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class StockDeliverActivity extends ToolBarBaseActivity {

    @BindView(R.id.rv_stock_delivery)
    RecyclerView mRvStock;
    private CommonAdapter mAdapter;
    private ArrayList<MyStockBean> mMyStockList;

    private Dialog mInputDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stock_deliver);
        ButterKnife.bind(this);
        setTitle(true, getString(R.string.delivery), getString(R.string.next));

        mMyStockList = (ArrayList<MyStockBean>) getIntent().getSerializableExtra("MyStockList");
        if (mMyStockList == null) {
            mMyStockList = new ArrayList<>();
        }
//        for (MyStockBean myStockBean : mMyStockList) {
//            myStockBean.setCount(myStockBean.getAmount());
//        }
        initRv();
    }

    private void initRv() {
        mAdapter = new CommonAdapter<MyStockBean>(mContext, mMyStockList, R.layout.item_goods_order) {
            @Override
            protected void convert(final ViewHolder holder, final MyStockBean stockBean, int position) {
                holder.setImageByUrl(mContext, R.id.iv_cover, Url.FILE_DOWNLOAD + stockBean.getCoverFid());
                holder.setText(R.id.tv_product_name, stockBean.getName());
                holder.setVisible(R.id.tv_total, true);
                holder.setText(R.id.tv_total,"x"+stockBean.getAmount());
                holder.setText(R.id.tv_amount, stockBean.getCount() + "");

                final TextView tvCount = holder.getView(R.id.tv_amount);
                holder.getView(R.id.tv_subtract).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        // 减号按钮，减少商品数量
                        int count = Integer.valueOf(tvCount.getText().toString());
                        if (count > 0) {
                            tvCount.setText(--count + "");
                            stockBean.setCount(count);
                        }
                    }
                });
                holder.getView(R.id.tv_add).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (stockBean.getCount() >= stockBean.getAmount()) {
                            return;
                        }
                        // 增加商品数量
                        int count = Integer.valueOf(tvCount.getText().toString());
                        tvCount.setText(++count + "");
                        stockBean.setCount(count);

                    }
                });
                holder.setOnClickListener(R.id.tv_amount, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showInputDialog(tvCount.getText().toString(), stockBean);
                    }
                });


                holder.setOnLongClickListener(R.id.cl_item, new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        showDeleteDialog(holder.getLayoutPosition());
                        return false;
                    }
                });
            }
        };

        mAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
              /*  Intent intent = new Intent(mContext, GoodsDetailActivity.class);
                startActivity(intent);*/
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

        int height = DisplayUtil.getInstance(mContext).dp2px(1);
        int color = Color.parseColor("#F4F4F4");
        RecycleViewDivider decoration = new RecycleViewDivider(mContext, MyDecoration.VERTICAL_LIST, height, color);
        mRvStock.addItemDecoration(decoration);
        mRvStock.setLayoutManager(new LinearLayoutManager(mContext));
        mRvStock.setAdapter(mAdapter);
    }

    private void showInputDialog(String count, final MyStockBean stockBean) {
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_input_num, null);
        mInputDialog = new AlertDialog.Builder(this).setView(view).create();
        final EditText et = view.findViewById(R.id.et_num);
        et.setText(count);
        et.setSelection(et.getText().length());

        ImageButton imbClose = view.findViewById(R.id.mbt_close);
        imbClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mInputDialog.dismiss();
            }
        });

        Button btnOk = view.findViewById(R.id.btn_ok);
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(et.getText().toString().trim())) {
                    ToastUtil.showMessage(mContext,"请输入数量");
                    return;
                }
                int count = Integer.valueOf(et.getText().toString().trim());
                if (count > stockBean.getAmount()) {
                    ToastUtil.showMessage(mContext,"数量不能大于库存数量");
                    return;
                }
                stockBean.setCount(count);
                mAdapter.notifyDataSetChanged();
                KeyboardUtils.hideSoftInput(et);
                mInputDialog.dismiss();
            }
        });

        Button btnCancel = view.findViewById(R.id.btn_cancel);
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mInputDialog.dismiss();
            }
        });
        mInputDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            public void onShow(DialogInterface dialog) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
            }

        });
        mInputDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                KeyboardUtils.hideSoftInput(et);
            }
        });

        mInputDialog.show();
    }

    private void showDeleteDialog(final int position) {
        ConfirmDialog dialog = new ConfirmDialog(mContext, new ConfirmDialog.OnBottomButtonClickListener() {
            @Override
            public void onLeftButtonClick(View view) {

            }

            @Override
            public void onRightButtonClick(View view) {
                mAdapter.notifyItemRemoved(position);
                mMyStockList.remove(position);
            }
        });
        dialog.showDialog("确认删除？", "取消", "确定");
    }

    @Override
    protected void onToolbarClicked(int paramId) {
        super.onToolbarClicked(paramId);
        switch (paramId) {
            case RIGHT_TEXT:
                 ArrayList<MyStockBean> list = new ArrayList<>();
                for (MyStockBean stockBean : mMyStockList) {
                    if (stockBean.getCount() > 0) {
                        list.add(stockBean);
                    }
                }

                if (list.isEmpty()) {
                    ToastUtil.showMessage(mContext,"发货的商品不能为空");
                    return;
                }
                Intent intent = new Intent(mContext, StockDeliverConfirmActivity.class);
                intent.putExtra("MyStockList", list);
                startActivity(intent);
                break;
        }
    }


}
